<?php 
$account = isset($_POST['account']) ? $_POST['account'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
$name = isset($_POST['name']) ? $_POST['name'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$method = isset($_POST['method']) ? $_POST['method'] : null;

require_once 'AdminAction.class.php';
$action = new AdminAction();
if($method == 'add'){
    $admin = new Admin();
    $admin -> account = $account;
    $admin -> password = $password;
    $admin -> name = $name;
    $admin -> email = $email;
    
    if($admin -> id = $action -> add($admin)){
        $admin = $action -> queryById($admin);
        $data = array("id" => 1,
                    "account" => $admin -> name,
                    "password" => $admin -> account,
                    "name" => $admin -> name,
                    "email" => $admin -> email);
        echo json_encode($data);
    } else {
        echo false;
    }
} 
if($method == 'query'){
    $admins = $action -> queryAll();
    if(count($admins)>0){
        
        echo json_encode($data);
    } else {
        echo false;
    }
} 
if($method == "create"){
    $admin = new Admin();
    $admin -> account = $account;
    $admin -> password = md5($password);
    $admin -> name = $name;
    $admin -> email = $email;
    
    if($action -> create($admin)){
        Header("Location: success.html");  
    } else {
        Header("Location: error.html");  
    }
} 
if($method == "login"){
    $admin = new Admin();
    $admin -> account = $account;
    $admin -> password = md5($password);
    $admin = $action -> login($admin);
    if($admin -> id >0){
        session_start();
        $_SESSION['admin'] = serialize($admin);//序列化对象
        $data = array("id" => $admin -> id,
                    "account" => $admin -> name,
                    "password" => $admin -> account,
                    "name" => $admin -> name,
                    "email" => $admin -> email);
        echo json_encode($data);
    } else {
        echo "false";
    }
} 
if($method == "logout"){
    session_start();
    unset($_SESSION['admin']);
}
?>